#!/bin/sh
#
# This script will generate a release on github/lfs-test-server.
# Ensure that you've bumped version in main.go, then run the script.
# The script does the following
#   * Ensure the build succeeds (and pulls the version from the build)
#   * Ensure the tests pass
#   * Cross compiles for supported platforms
#   * Creates the release on github/lfs-test-server
#   * Uploads binary assets to the release.


go build -o lfs-test-server
rc=$?; if [[ $rc != 0 ]]; then echo "Build failed."; exit $rc; fi
version=$(./lfs-test-server -v)

while true; do
  read -p "Release version $version? [y/n] " yn
  case $yn in
    [Yy]* ) break;;
    [Nn]* ) exit;;
    * ) echo "Please answer yes or no.";;
  esac
done

# Make sure tests pass
echo "Running tests..."
go test
rc=$?; if [[ $rc != 0 ]]; then echo "Tests failed, cannot release."; exit $rc; fi

# Build all files
rm -rf dist
mkdir dist

echo "Building darwin amd64"
mkdir -p dist/lfs-test-server-darwin-amd64
GOOS=darwin GOARCH=amd64 go build -o dist/lfs-test-server-darwin-amd64/lfs-test-server
cp README.md dist/lfs-test-server-darwin-amd64
cp LICENSE dist/lfs-test-server-darwin-amd64
cd dist && tar zcf lfs-test-server-darwin-amd64-$version.tar.gz lfs-test-server-darwin-amd64; cd ..

echo "Building linux 386"
mkdir -p dist/lfs-test-server-linux-386
GOOS=linux GOARCH=386 go build -o dist/lfs-test-server-linux-386/lfs-test-server
cp README.md dist/lfs-test-server-linux-386
cp LICENSE dist/lfs-test-server-linux-386
cd dist && tar zcf lfs-test-server-linux-386-$version.tar.gz lfs-test-server-linux-386; cd ..

echo "Building linux amd64"
mkdir -p dist/lfs-test-server-linux-amd64
GOOS=linux GOARCH=amd64 go build -o dist/lfs-test-server-linux-amd64/lfs-test-server
cp README.md dist/lfs-test-server-linux-amd64
cp LICENSE dist/lfs-test-server-linux-amd64
cd dist && tar zcf lfs-test-server-linux-amd64-$version.tar.gz lfs-test-server-linux-amd64; cd ..

echo "Building freebsd 386"
mkdir -p dist/lfs-test-server-freebsd-386
GOOS=freebsd GOARCH=386 go build -o dist/lfs-test-server-freebsd-386/lfs-test-server
cp README.md dist/lfs-test-server-freebsd-386
cp LICENSE dist/lfs-test-server-freebsd-386
cd dist && tar zcf lfs-test-server-freebsd-386-$version.tar.gz lfs-test-server-freebsd-386; cd ..

echo "Building freebsd amd64"
mkdir -p dist/lfs-test-server-freebsd-amd64
GOOS=freebsd GOARCH=amd64 go build -o dist/lfs-test-server-freebsd-amd64/lfs-test-server
cp README.md dist/lfs-test-server-freebsd-amd64
cp LICENSE dist/lfs-test-server-freebsd-amd64
cd dist && tar zcf lfs-test-server-freebsd-amd64-$version.tar.gz lfs-test-server-freebsd-amd64; cd ..

echo "Building windows 386"
mkdir -p dist/lfs-test-server-windows-386
GOOS=windows GOARCH=386 go build -o dist/lfs-test-server-windows-386/lfs-test-server.exe
cp README.md dist/lfs-test-server-windows-386
cp LICENSE dist/lfs-test-server-windows-386
cd dist && zip -q -j lfs-test-server-windows-386-$version.zip lfs-test-server-windows-386/*; cd ..

echo "Building windows amd64"
mkdir -p dist/lfs-test-server-windows-amd64
GOOS=windows GOARCH=amd64 go build -o dist/lfs-test-server-windows-amd64/lfs-test-server.exe
cp README.md dist/lfs-test-server-windows-amd64
cp LICENSE dist/lfs-test-server-windows-amd64
cd dist && zip -q -j lfs-test-server-windows-amd64-$version.zip lfs-test-server-windows-amd64/*; cd ..

# Create the release
tmpl=`mktemp lfs-test-server-release.XXXXXXXXX`
out=`mktemp lfs-test-server-out.XXXXXXXX`
payload=$(cat <<EOF
{
  "tag_name": "v$version",
  "target_commitish": "master",
  "name": "Release $version",
  "draft": false,
  "prerelease": false,
  "body": ""
}
EOF
)
echo "$payload" > $tmpl

${EDITOR:-vim} $tmpl

curl -n -X POST -d @$tmpl -o $out https://api.github.com/repos/git-lfs/lfs-test-server/releases
id=$(cat $out | jq -r ".id")

if [[ $id == "null" ]]; then echo "Failed creating release."; cat $out; exit 1; fi
echo "Created release id: $id"


# Upload each file to the release
upload=$(cat $out | jq -r ".upload_url" | sed s/"{?name}"//)
echo "Uploading lfs-test-server-darwin-amd64-$version.tar.gz"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-darwin-amd64-$version.tar.gz "$upload?name=lfs-test-server-darwin-amd64-$version.tar.gz&label=Mac%20AMD64"

echo "Uploading lfs-test-server-linux-386-$version.tar.gz"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-linux-386-$version.tar.gz "$upload?name=lfs-test-server-linux-386-$version.tar.gz&label=Linux%20386"

echo "Uploading lfs-test-server-linux-amd64-$version.tar.gz"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-linux-amd64-$version.tar.gz "$upload?name=lfs-test-server-linux-amd64-$version.tar.gz&label=Linux%20AMD64"

echo "Uploading lfs-test-server-freebsd-386-$version.tar.gz"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-freebsd-386-$version.tar.gz "$upload?name=lfs-test-server-freebsd-386-$version.tar.gz&label=FreeBSD%20386"

echo "Uploading lfs-test-server-freebsd-amd64-$version.tar.gz"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-freebsd-amd64-$version.tar.gz "$upload?name=lfs-test-server-freebsd-amd64-$version.tar.gz&label=FreeBSD%20AMD64"

echo "Uploading lfs-test-server-windows-386-$version.zip"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-windows-386-$version.zip "$upload?name=lfs-test-server-windows-386-$version.zip&label=Windows%20386"

echo "Uploading lfs-test-server-windows-amd64-$version"
curl -n -o $out -H "Content-Type: application/octet-stream" -X POST --data-binary @dist/lfs-test-server-windows-amd64-$version.zip "$upload?name=lfs-test-server-windows-amd64-$version.zip&label=Windows%20AMD64"

rm -f $tmpl $out
rm -rf dist
